<?php
namespace app\controller;

use think\facade\Db;

use app\middleware\UserAuth;

class Common
{
	/**
	*
	* 全局认证中间件
	*/
	protected $middleware = [UserAuth::class];


	/**
	 *  获取用户真实地址
	 *
	 * @return    string  返回用户ip
	 */
	function GetIP() {
			static $realip = NULL;
			if ($realip !== NULL)
			{
					return $realip;
			}
			if (isset($_SERVER))
			{
					if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
					{
							$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
							/* 取X-Forwarded-For中第x个非unknown的有效IP字符? */
							foreach ($arr as $ip)
							{
									$ip = trim($ip);
									if ($ip != 'unknown')
									{
											$realip = $ip;
											break;
									}
							}
					}
					elseif (isset($_SERVER['HTTP_CLIENT_IP']))
					{
							$realip = $_SERVER['HTTP_CLIENT_IP'];
					}
					else
					{
							if (isset($_SERVER['REMOTE_ADDR']))
							{
									$realip = $_SERVER['REMOTE_ADDR'];
							}
							else
							{
									$realip = '0.0.0.0';
							}
					}
			}
			else
			{
					if (getenv('HTTP_X_FORWARDED_FOR'))
					{
							$realip = getenv('HTTP_X_FORWARDED_FOR');
					}
					elseif (getenv('HTTP_CLIENT_IP'))
					{
							$realip = getenv('HTTP_CLIENT_IP');
					}
					else
					{
							$realip = getenv('REMOTE_ADDR');
					}
			}
			preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
			$realip = ! empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';
			return $realip;
	}

	/**
	* 写日志方法
	*
	* @return
	*/
  protected function cfg_write_logs( $request, $cookie, $companyId, $nickname, $username, $operType ) {
		$browser = new \Browser\Browser();
		Db::table('cfg_logs')->insert([
			'browser'=> $browser->getBrowser(),
			'device'=> $browser->getPlatform(),
			'referer'=> $request->header('referer'),
			'method'=> $request->method(),
			'url'=> $request->url(),
			'ip'=> $this->GetIP(),
			'cookie'=> $cookie,
			'companyId' => $companyId,
      'nickname' => $nickname,
      'user' => $username,
			'operType'=> $operType,
			'type'=> 1,
			'time'=> time() ]);
  }

	/**
	* 写操作方法
	*
	* @return
	*/
  protected function cfg_write_oper( $request, $cookie, $companyId, $nickname, $username, $operType, $model, $description, $state, $param ) {
		$browser = new \Browser\Browser();
		Db::table('cfg_logs')->insert([
			'browser'=> $browser->getBrowser(),
			'device'=> $browser->getPlatform(),
			'referer'=> $request->header('referer'),
			'method'=> $request->method(),
			'operMethod' => $request->baseUrl(),
			'url'=> $request->url(),
			'ip'=> $this->GetIP(),
			'cookie'=> $cookie,
			'description'=> $description,
			'companyId' => $companyId,
      'nickname' => $nickname,
      'user' => $username,
			'operType'=> $operType,
			'model'=> $model,
			'param' => $param,
			'state'=> $state,
			'type'=> 2,
			'time'=> time() ]);
  }

	/**
	* 获取角色权限组
	* @return
	*/
  protected function cfg_getRoleValue ( $uid ) {
		$query = Db::table('cfg_user')->where('id', $uid)->value('role');
		if(empty($query)){
			return false;
		}
		$query = Db::table('cfg_role')->where('id', $query)->value('roleValue');
		if(empty($query)){
			return false;
		}
		return $query;
  }

	/**
	* 权限验证
	* 1 验证方法, 2验证按钮
	* @return
	*/
  protected function hasPermission ( $userArray, $authority) {
		if($userArray['level'] < 3) {
			return false;
		}
		$roleValue = $this->cfg_getRoleValue($userArray['id']);
		if( $roleValue == false ) {
			return true;
		}
		$where = [];
    array_push($where, ['menuId', 'in', $roleValue]);
		array_push($where, ['authority', '=', $authority]);
		$menuId = Db::table('cfg_menu')->where($where)->value('menuId');
		if(empty($menuId)) {
			return true;
		}
		return false;
  }


}
